<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/3/21
 * Time: 10:09
 * 发现
 */
namespace app\api\controller;
use think\Db;
class Fight extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
        
    }
    /***
     * day
     * 知识对战——我能不能参与
     */
    public function fightYesNo(){
        extract(input());
        $seesee = $this->getUserStatusForSee($user_id);
        if($seesee['status'] != 200){
            return json($seesee);
        }
        $score = Db::table('up_sign_config')->where('id',4)->value('score');
        $user = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->field('user_integral,user_type,user_wanshan')->find();
        $my = $user['user_integral'];
        if($my < $score){
            return json(array('status' => 0, 'msg' => '喵币不足'.$score.',不能参与知识对战！'));
        }else{
            if($user['user_type'] == 2 && $user['user_wanshan'] != 2){
                return json(array('status' => 0, 'msg' => '老师请先去玩完善资料'));
            }
//            if(isset($day) && $day == 1){
//                $mylevel = Db::table('up_u_user')
//                    ->where('user_id',$user_id)
//                    ->value('user_level');
//                if($mylevel < 2){
//                    return json(array('status' => 0, 'msg' => '等级不足'));
//                }
//            }
            return json(array('status' => 200, 'msg' => 'ok'));
        }

    }
    /***
     *
     * 知识对战——获取人的信息
     */
    public function getIdInfo(){
        extract(input());
        $friends = Db::table('up_u_user u')
            ->join ('up_exp_level l','u.user_level = l.el_id','LEFT')
            ->where('user_id',$uid)
            ->field('user_id,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win,el_name')
            ->find();
        return json(array('status' => 200, 'msg' => $friends));
    }
    /***
     *
     * 知识对战——获取俩人的信息
     */
    public function getTwoIdInfo(){
        extract(input());
//        $uid = 38;
        $other = Db::table('up_u_user u')
            ->join ('up_exp_level l','u.user_level = l.el_id','LEFT')
            ->join ('up_grade g','u.user_grade = g.grade_id','LEFT')
            ->where('user_id',$uid)
            ->field('user_id,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win,el_name,user_type,grade_name')
            ->find();
        $my = Db::table('up_u_user u')
            ->join ('up_exp_level l','u.user_level = l.el_id','LEFT')
            ->join ('up_grade g','u.user_grade = g.grade_id','LEFT')
            ->where('user_id',$user_id)
            ->field('user_id,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win,el_name,user_type,grade_name')
            ->find();
        if($other['user_type'] == 2){
            $other['grade_name'] = '老师';
        }
        if($my['user_type'] == 2){
            $my['grade_name'] = '老师';
        }
        return json(array('status' => 200, 'other' => $other, 'my' => $my));
    }

    /***
     * 最后加的：
     * 知识对战——这个人有没有把我拉黑
     */
    public function getUidStatus(){
        extract(input());
        $black = Db::table('up_friend')
            ->where('friend_user_id',$user_id)
            ->where('friend_other_user',$uid)
            ->find();
        if($black && $black['friend_status'] == 4){
            //200可以收到邀请
            return json(array('status' => 100, 'msg' => 'ok'));
        }else {
            return json(array('status' => 200, 'msg' => 'ok'));
        }

    }
    /***
     *
     * 知识对战——好友对战(获取好友列表)
     */
    public function fightFriend(){
        extract(input());
        $score = Db::table('up_sign_config')->where('id',4)->value('score');
        $where1 = array();
        $where1['friend_user_id'] = $user_id;
        $where1['friend_status'] = 1;
        $where1['user_integral'] = array('>=',$score);
        $where1['user_f_online'] = 1;
        $where1['user_type'] = 1;
        $where2 = array();
        $where2['friend_user_id'] = $user_id;
        $where2['friend_status'] = 1;
        $where2['user_integral'] = array('>=',$score);
        $where2['user_f_online'] = 1;
        $where2['user_type'] = 2;
        $where2['user_wanshan'] = 2;
        $friends = Db::table('up_friend f')
            ->join ('up_u_user u','f.friend_other_user = u.user_id','LEFT')
            ->join ('up_exp_level l','u.user_level = l.el_id','LEFT')
            ->where(function ($q) use ($where1){
                $q->where($where1);
            })
            ->whereOr(function ($q) use ($where2){
                $q->where($where2);
            })
            ->field('user_id,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win,el_name')
            ->page($page,20)
            ->select();
        return json(array('status' => 200, 'msg' => $friends));
    }

    /**
     * 單獨為好友對戰做的消息推送（專用）
     */
    public function fightFriendTuisong(){
        extract(input());
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_grade,user_type,user_username')->find();
        $user_name = $user['user_username'];
        if(mb_strlen($user_name, 'utf-8') > 14){
            $user_name = mb_substr($user_name, 0, 14) . '...';
        }
        $str = '喵喵阅读:' . $user_name . '向您发起对战邀请';
        $user_to_id = (string)$touid;
        try{
            $this->Jpush(1, "$user_to_id", $str, ['send_type' => 4]);
        }catch(\Exception $e){

        }
        return json(array('status' => 200, 'msg' => '已推送'));
    }

    /***
     * high：如果存在且为1，则匹配高年级
     * 知识对战——随机对战(获取其中一个)（旧版本）
     */
    public function fightGetoneRand(){
        extract(input());
//        $user_id = 56;
        $score = Db::table('up_sign_config')->where('id',4)->value('score');
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_grade,user_type,user_username')->find();
        $grade = $user['user_grade'];
        $user_type = $user['user_type'];
        $where = array();
        $where['user_integral'] = array('>=',$score);
        $where['user_status'] = array('in','1,2,5');
        $where['user_f_online'] = 1;
        $where['user_type'] = $user_type;
        $blacks = Db::table('up_friend')
            ->where('friend_user_id',$user_id)
            ->where('friend_status',4)
            ->column('friend_other_user');
        if($blacks){
            $blacks[] = $user_id;
            $where['user_id'] = array('not in',$blacks);
        }else{
            $where['user_id'] = array('<>',$user_id);
        }
        $friends = array();
        if($user_type == 1){
            //是否匹配高年级
            if(isset($high) && $high == 1){
                $where['user_grade'] = array('>',$grade);
            }else{
                $where['user_grade'] = $grade;
            }
            //-----------随机start---------------
            $ids = Db::table('up_u_user')
                ->where($where)
                ->limit(0,10000)
                ->column('user_id');
            if($ids){
                $idk = array_rand($ids,1);
                $id = $ids[$idk];
                $friends = Db::table('up_u_user')
                    ->where('user_id',$id)
                    ->field('user_id,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win')
                    ->find();
            }else{
                if(isset($high) && $high == 1 && empty($ids)){
                    unset($where['user_grade']);
                    $where['user_grade'] = $grade;
                    $ids = Db::table('up_u_user')
                        ->where($where)
                        ->limit(0,10000)
                        ->column('user_id');
                    if($ids){
                        $idk = array_rand($ids,1);
                        $id = $ids[$idk];
                        $friends = Db::table('up_u_user')
                            ->where('user_id',$id)
                            ->field('user_id,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win')
                            ->find();
                    }
                }
            }
            //-----------随机end---------------
        }else{
            $ids = Db::table('up_u_user')
                ->where($where)
                ->limit(0,10000)
                ->column('user_id');
            if($ids){
                $idk = array_rand($ids,1);
                $id = $ids[$idk];
                $friends = Db::table('up_u_user')
                    ->where('user_id',$id)
                    ->field('user_id,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win')
                    ->find();
            }
        }
        if($friends){
            $user_name = $user['user_username'];
            if(mb_strlen($user_name, 'utf-8') > 14){
                $user_name = mb_substr($user_name, 0, 14) . '...';
            }
            $str = '喵喵阅读:' . $user_name . '向您发起对战邀请';
            $tostr = $friends['user_id'];
            $user_to_id = (string)$tostr;
            try{
                $this->Jpush(1, "$user_to_id", $str, ['send_type' => 4]);
            }catch(\Exception $e){

            }
            return json(array('status' => 200, 'msg' => $friends));
        }else{
            return json(array('status' => 100, 'msg' => '找不到合适的用户'));
        }

    }

    /**
     * 可以挑战之后先调一下进入队列
     * @return \think\response\Json
     */
    public function fightRandIn()
    {
        extract(input());
        $da = Db::table('up_fight_list')->where('new_user_id', $user_id)->find();
        if($da){
            Db::table('up_fight_list')->where('new_user_id', $user_id)->delete();
        }
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_grade,user_type')->find();
        $insert = [
            'new_user_id'=>$user_id,
            'new_grade'=>$user['user_grade'],
            'new_type'=>$user['user_type'],
            'new_time'=>time(),
        ];
        if(isset($high) && $high == 1){
            $insert['new_high'] = 2;
        }
        $res = Db::table('up_fight_list')->insert($insert);
        if($res){
            $now_30 = time() - 30;
            Db::table('up_fight_list')
                ->where('new_status', 1)
                ->where('new_time', '<=',$now_30)
                ->setField('new_status',2);
            return json(array('status' => 200, 'msg' => 'ok'));
        }else{
            return json(array('status' => 100, 'msg' => '初始化失败，请重新进入'));
        }
    }
    /**
     * 退出挑战
     * @return \think\response\Json
     */
    public function fightRandOut()
    {
        extract(input());
        Db::table('up_fight_list')->where('new_user_id', $user_id)->delete();
        return json(array('status' => 200, 'msg' => 'ok'));
    }
    /***
     * high：如果存在且为1，则匹配高年级
     * 知识对战——随机对战(获取其中一个)（新版本）
     */
    public function fightGetoneRandNew(){
        extract(input());
        $data = Db::table('up_fight_list')->where('new_user_id', $user_id)->find();
        if($data['new_time']+30 <= time()){
            Db::table('up_fight_list')->where('new_user_id', $user_id)->setField('new_status',2);
            return json(array('status' => 100, 'msg' => '等待超时'));
        }else{
            if($data['new_status'] == 2){
                return json(array('status' => 101, 'msg' => '已经找到了'));
            }
        }
        $now_30 = time() - 30;
        if($data['new_type'] == 1){
            $where1 = array();
            $where2 = array();
            $where1['new_status'] = 1;
            $where1['new_time'] = ['>',$now_30];
            $where1['new_type'] = 1;
            $where1['new_user_id'] = ['<>',$user_id];
            $where1['new_grade'] = $data['new_grade'];

            $where2['new_status'] = 1;
            $where2['new_time'] = ['>',$now_30];
            $where2['new_type'] = 1;
            $where2['new_user_id'] = ['<>',$user_id];
            $where2['new_high'] = 2;
            $where2['new_grade'] = ['<=',$data['new_grade']];
            //是否匹配高年级
            if($data['new_high'] == 2){ //我允许不允许匹配高年级的区别就在于：允不允许大于我的年级参与
                $where1['new_grade'] = ['>=',$data['new_grade']];
            }
            $res = Db::table('up_fight_list')
                ->where(function ($query) use ($where1){
                    $query->where($where1);
                })
                ->whereOr(function ($query) use ($where2) {
                    $query->where($where2);
                })
                ->order('new_id ASC')
                ->find();
            //-----------随机end---------------
        }else{
            $where1 = array();
            $where1['new_status'] = 1;
            $where1['new_time'] = ['>',$now_30];
            $where1['new_type'] = 2;
            $where1['new_user_id'] = ['<>',$user_id];
            $res = Db::table('up_fight_list')
                ->where($where1)
                ->order('new_id ASC')
                ->find();
        }
        if($res){
            $msg['user_id'] = $user_id;
            $msg['user_id_other'] = $res['new_user_id'];
            $subjects = $this->getFightSubjectsNew($user_id,$res['new_user_id']);
            if($subjects === false){
                //在题目不够的情况下有可能该你匹配的没匹配上，下一个匹配上了
                return json(array('status' => 0, 'msg' => '题目不够'));
            }
            Db::table('up_fight_list')->where('new_user_id','in',"{$user_id},{$res['new_user_id']}")->setField('new_status',2);
            $config = Db::table('up_sign_config')->where('id',6)->find();
            return json(array('status' => 200, 'users' => $msg, 'msg' => $subjects, 'config' => $config));
        }else{
            return json(array('status' => 0, 'msg' => '找不到合适的用户'));
        }

    }

    //--------------------------------------------知识对战----------------------------------------------

    /***
     * day：若存在此参且为1，则代表是每日挑战需要的题，固定5个
     * uid：对方的id
     * high：空和非空（高年级）
     * 获取对战的题
     */
    public function getFightSubjects(){
        extract(input());
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_grade,user_type')->find();
        $userType = $user['user_type'];
        $config = Db::table('up_sign_config')->where('id',6)->find();
        $num = $config['num'];
        $mygrade = array();
        $mygrade[] = $user['user_grade'];
        if($userType == 2){
            $mygrade = Db::table('up_grade_class')
                ->where('gs_user_id',$user_id)->group('gs_grade_id')
                ->column('gs_grade_id');
            if(!$mygrade){
                //一般不会可能这种情况
                return json(array('status' => 100, 'msg' => '此老师找不到年级', 'config' => ''));
            }
        }
        $allids = Db::table('up_fight_subject')->where('fs_status',1)->where('fs_grade','in',$mygrade)->column('fs_id');
        //如果是好友对战
        if(isset($uid) && $uid){
            $user2 = Db::table('up_u_user')->where('user_id',$uid)
                ->field('user_grade,user_type,user_wanshan')->find();
            if($user2['user_type'] == 1 && !in_array($user2['user_grade'],$mygrade)){
                $mygrade[] = $user2['user_grade'];
                $allids = Db::table('up_fight_subject')->where('fs_status',1)->where('fs_grade','in',$mygrade)->column('fs_id');
            }
            if($user2['user_type'] == 2 && $user2['user_wanshan'] == 2){
                $grades = Db::table('up_grade_class')
                    ->where('gs_user_id',$uid)->group('gs_grade_id')
                ->column('gs_grade_id');
                if($grades){
                    $grades = array_merge($grades,$mygrade);
                }else{
                    $grades = $mygrade;
                }
                $allids = Db::table('up_fight_subject')->where('fs_status',1)->where('fs_grade','in',$grades)->column('fs_id');
            }
        }
        //如果是随机对战（匹配高年级）
        if($userType == 1 && isset($high) && $high){
            $allids = Db::table('up_fight_subject')->where('fs_status',1)->where('fs_grade','>=',$user['user_grade'])->column('fs_id');

        }

        if(isset($day) && $day == 1){
            $num = 5;
            $config = Db::table('up_sign_config')->where('id',7)->find();
        }
        $ids = array();
        if(count($allids) > $num){
            $keys = array_rand($allids,$num);
            foreach ($keys as $v){
                $ids[] = $allids[$v];
            }
        }else{
            $ids = $allids;
        }
        //如果题目不够，直接返回错误信息
        if(count($allids) < $num){
            return json(array('status' => 100, 'msg' => '题目不够', 'config' => ''));
        }
        //end
        $data = Db::table('up_fight_subject')
            ->where('fs_id','in',$ids)
            ->select();

        return json(array('status' => 200, 'msg' => $data, 'config' => $config));
    }
    /***
     *
     * 获取对战的题(内部使用，为新的使用)
     */
    public function getFightSubjectsNew($user_id,$user_id_other){
        extract(input());
        $my = Db::table('up_fight_list')->where('new_user_id', $user_id)->find();
        $ta = Db::table('up_fight_list')->where('new_user_id', $user_id_other)->find();
        $userType = $my['new_type'];
        $config = Db::table('up_sign_config')->where('id',6)->find();
        $num = $config['num'];
        if($userType == 1){
            $allids = Db::table('up_fight_subject')
                ->where('fs_status',1)
                ->where('fs_grade','in',"{$my['new_grade']},{$ta['new_grade']}")
                ->column('fs_id');
        }else{
            $allids = Db::table('up_fight_subject')
                ->where('fs_status',1)
                ->column('fs_id');
        }
        $ids = array();
        if(count($allids) > $num){
            $keys = array_rand($allids,$num);
            foreach ($keys as $v){
                $ids[] = $allids[$v];
            }
        }else{
            $ids = $allids;
        }
        //如果题目不够，直接返回错误信息
        if(count($allids) < $num){
            return false;
        }
        //end
        $data = Db::table('up_fight_subject')
            ->where('fs_id','in',$ids)
            ->select();
        return $data;
    }
    /***
     * 答错了要入错题本
     */
    public function errorBook(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $data = array();
        $data['er_user_id'] = $user_id;
        $data['er_fs_id'] = $fs_id;
        $have = Db::table('up_error_book')->where($data)->find();
        if($have){
            $update = [
                'er_time'=>$today,
                'er_num'=>$have['er_num']+1,
                'er_answer'=>$answer
            ];
            Db::table('up_error_book')->where($data)->update($update);
        }else{
            $data['er_answer'] = $answer;
            $data['er_time'] = $today;
            $data['er_num'] = 1;
            Db::table('up_error_book')->insert($data);
        }
        return json_encode(['status'=>200,'msg'=>'ok']);
    }
    /***
     * 对战成功者：勋章、得奖励、升级
     * ji：我对了几道题
     * lose_user_id：失败的人的id
     */
    public function fightWin(){
        extract(input());
//        $rg_id = 3;$user_id=143;
        $config = Db::table('up_sign_config')->where('id',6)->find();
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_exp,user_f_continu')->find();
        $exp1 = $user['user_exp'];
        $level_before = Db::table('up_exp_level')
            ->where('el_min','<=',$exp1)
            ->order('el_level DESC')
            ->find();
        // 启动事务
        Db::startTrans();
        try{
            //勋章
            $gameTrue = $this->getHonorForEvery ($user_id,5);
            if($user['user_f_continu'] == 4){
                $gameTrue = $this->getHonorForEvery ($user_id,6);
            }
            if($user['user_f_continu'] == 9){
                $gameTrue = $this->getHonorForEvery ($user_id,7);
            }
            if($gameTrue === false){
                $honor = '';
            }else{
                $honor = $gameTrue;
            }
            //1-1、先涨我的经验和金币
            $getexp = round(($config['exp']/$config['num'])*$ji);
            $getscore = round(($config['score']/$config['num'])*$ji);
            $losssco = Db::table('up_u_user')->where('user_id',$lose_user_id)->value('user_integral');
            if($losssco < $getscore){
                $getscore = $losssco;
            }
            $coupon_price = $this->addUserExp ($user_id,$getexp,$getscore,2);
            if($coupon_price === false){
                $coupon_price = '';
            }
            //1-2、只减去失败者的金币（切记不能加经验）
            $losescore = $getscore;
            $this->addUserExp ($lose_user_id,0,$losescore,3);
            //2、是否升级(放到最后，因为要涨完再比)
            $newexp = Db::table('up_u_user')->where('user_id',$user_id)->value('user_exp');
            $level_up = $this->levelUp($user_id,$newexp,$level_before['el_level']);
            if($level_up === false){
                $shengji = '';
            }else{
                $shengji = $level_up;
            }
            // 提交事务
            $two = $this->getTwoIdInfoForInside($user_id,$lose_user_id);
            Db::commit();
            $returnData['status'] = 200;
            $returnData['msg'] = '胜利';
            $returnData['honor'] = $honor;
            $returnData['shengji'] = $shengji;
            $returnData['coupon_price'] = $coupon_price;
            $returnData['can'] = 1;//可以继续对战
            $returnData['my'] = $two['my'];//我的信息
            $returnData['other'] = $two['other'];//输家的信息
            $returnData['expexp'] = $getexp;//我得了多少经验
            $returnData['scorescore'] = $getscore;//我得了多少金币
            return json_encode($returnData);
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /***
     * 对战失败者：得奖励、升级
     * ji：对方对了几道题！！！
     * win_user_id：赢家的信息（返回用的）
     */
    public function fightLose(){
        extract(input());
//        $rg_id = 3;$user_id=143;
        $config = Db::table('up_sign_config')->where('id',6)->find();
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_exp,user_f_continu')->find();
        $exp1 = $user['user_exp'];
        $level_before = Db::table('up_exp_level')
            ->where('el_min','<=',$exp1)
            ->order('el_level DESC')
            ->find();
        // 启动事务
        Db::startTrans();
        try{
            //1、只涨经验（金币在胜利方扣过了）
            $getexp = round((($config['exp']/$config['num'])*$ji)/2);
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_exp',$getexp);
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_exp_week',$getexp);
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_exp_month',$getexp);
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_exp_year',$getexp);
            $losescore = round(($config['score']/$config['num'])*$ji);
            $have = Db::table('up_u_user')->where('user_id',$user_id)->value('user_integral');
//            if($have < $losescore){
//                $losescore = $have;
//            }
//            $this->addUserExp ($user_id,$getexp,$losescore,3);

            //2、是否升级(放到最后，因为要涨完再比)
            $newexp = Db::table('up_u_user')->where('user_id',$user_id)->value('user_exp');
            $level_up = $this->levelUp($user_id,$newexp,$level_before['el_level']);
            if($level_up === false){
                $shengji = '';
            }else{
                $shengji = $level_up;
            }
            // 提交事务
            $two = $this->getTwoIdInfoForInside($user_id,$win_user_id);
            Db::commit();
            $returnData['status'] = 200;
            $returnData['msg'] = '计算结束';
            $returnData['shengji'] = $shengji;
            $returnData['can'] = 1;//可以继续对战
            $returnData['my'] = $two['my'];//我的信息
            $returnData['other'] = $two['other'];//赢家的信息
            $returnData['expexp'] = $getexp;//我得了多少经验
            $returnData['scorescore'] = $losescore;//我得了多少金币
            $can = Db::table('up_sign_config')->where('id',4)->value('score');
            if($have-$losescore < $can){
                $returnData['can'] = 2;//不可以继续对战
            }
            return json_encode($returnData);
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'异常']);
        }
    }


    /***
     * 每日挑战胜利者：得奖励、升级
     *
     */
    public function dayFightWin(){
        extract(input());
        $config = Db::table('up_sign_config')->where('id',7)->find();
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_exp')->find();
        $exp1 = $user['user_exp'];
        $level_before = Db::table('up_exp_level')
            ->where('el_min','<=',$exp1)
            ->order('el_level DESC')
            ->find();
        // 启动事务
        Db::startTrans();
        try{
            //1、先涨
            $this->addUserExp ($user_id,$config['exp'],$config['score'],4);

            //2、是否升级(放到最后，因为要涨完再比)
            $newexp = Db::table('up_u_user')->where('user_id',$user_id)->value('user_exp');
            $level_up = $this->levelUp($user_id,$newexp,$level_before['el_level']);
            if($level_up === false){
                $shengji = '';
            }else{
                $shengji = $level_up;
            }
            // 提交事务
            Db::commit();
            $returnData['status'] = 200;
            $returnData['msg'] = '计算结束';
            $returnData['shengji'] = $shengji;
            return json_encode($returnData);
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'异常']);
        }
    }

    /***
     *
     * 知识对战——获取俩人的信息(自己内部用)
     */
    public function getTwoIdInfoForInside($user_id,$uid){
        $other = Db::table('up_u_user u')
            ->join ('up_exp_level l','u.user_level = l.el_id','LEFT')
            ->join ('up_grade g','u.user_grade = g.grade_id','LEFT')
            ->where('user_id',$uid)
            ->field('user_id,user_type,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win,el_name,grade_name')
            ->find();
        if($other['user_type'] == 2){
            $other['grade_name'] = '老师';
        }
        $my = Db::table('up_u_user u')
            ->join ('up_exp_level l','u.user_level = l.el_id','LEFT')
            ->join ('up_grade g','u.user_grade = g.grade_id','LEFT')
            ->where('user_id',$user_id)
            ->field('user_id,user_type,user_username,user_image,user_age,user_gender,user_birthday,user_f_all,user_f_win,el_name,grade_name')
            ->find();
        if($my['user_type'] == 2){
            $my['grade_name'] = '老师';
        }
        $res = [
            'other' => $other,
            'my' => $my
        ];
        return $res;
    }
    /***
     *
     * 退出登录
     */
    public function getOut(){
        extract(input());
        Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->setField('user_f_online',2);
        return json(['status'=>200]);
    }
}